﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadLocal_T
{
   public static class Program

    {

       /*Here you see another feature of the .NET Framework. You can use the Thread.Current-
Thread class to ask for information about the thread that’s executing. This is called the 
thread’s execution context. This property gives you access to properties like the thread’s 
current culture (a CultureInfo associated with the current thread that is used to format dates, 
times, numbers, currency values, the sorting order of text, casing conventions, and string 
comparisons), principal (representing the current security context), priority (a value to indicate 
how the thread should be scheduled by the operating system), and other info.
When a thread is created, the runtime ensures that the initiating thread’s execution context 
is flowed to the new thread. This way the new thread has the same privileges as the parent 
thread.
This copying of data does cost some resources, however. If you don’t need this data, you 
can disable this behavior by using the ExecutionContext.SuppressFlow method.
        * */
        public static ThreadLocal<int> _field = 

            new ThreadLocal<int>(() => 

            {

                return Thread.CurrentThread.ManagedThreadId;

            });

        public static void Main()

        {

            new Thread(() =>

                { 

                    for(int x = 0; x < _field.Value; x++)

                    {

                        Console.WriteLine("Thread A: {0}", x);

                    }

                }).Start();

            new Thread(() =>

                {

                    for (int x = 0; x < _field.Value; x++)

                    {

                        Console.WriteLine("Thread B: {0}", x);

                    }
                    }).Start();

            Console.ReadKey();

        }

    }

}
